package com.example.b_308.myapplication.view;

import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.b_308.myapplication.R;
import com.example.b_308.myapplication.comment.view.TitleBar;
import com.example.b_308.myapplication.controller.UserController;
import com.example.b_308.myapplication.model.Staff;
import com.example.b_308.myapplication.view.base.BaseActivity;
import com.example.b_308.myapplication.view.user.IUserView;

/**
 * Created by B-308 on 2017/11/22.
 */
public class RegisterActivity extends BaseActivity implements View.OnClickListener, IUserView {
    EditText et_phone, et_phone_code, et_doctor_code, et_password1, et_password2;
    ImageView iv_return, iv_phone_code_delete, iv_doctor_code_delete, iv_password1_delete, iv_password2_delete;
    Button but_getCode_again, but_register;
    TextView tv_setting;
    UserController userController;
    TitleBar titleBar;
String phone;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        phone=getIntent().getStringExtra("phone");
        initView();
        initDate();
        initListener();
    }

    private void initListener() {
        iv_return.setOnClickListener(this);
        iv_phone_code_delete.setOnClickListener(this);
        iv_doctor_code_delete.setOnClickListener(this);
        iv_password1_delete.setOnClickListener(this);
        iv_password2_delete.setOnClickListener(this);
        but_getCode_again.setOnClickListener(this);
        but_register.setOnClickListener(this);
        et_phone_code.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 0) {
                    iv_phone_code_delete.setVisibility(View.GONE);
                } else {
                    iv_phone_code_delete.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        et_doctor_code.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 0) {
                    iv_doctor_code_delete.setVisibility(View.GONE);
                } else {
                    iv_doctor_code_delete.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        et_password1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 0) {
                    iv_password1_delete.setVisibility(View.GONE);
                } else {
                    iv_password1_delete.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        et_password2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 0) {
                    iv_password2_delete.setVisibility(View.GONE);
                } else {
                    iv_password2_delete.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void initDate() {
        userController = new UserController(this);
    }


    private void initView() {
        iv_return = findView(R.id.iv_return);
        tv_setting = findView(R.id.tv_setting);
        tv_setting.setVisibility(View.GONE);
        et_phone = findView(R.id.et_phone);
        et_phone_code = findView(R.id.et_phone_code);
        et_doctor_code = findView(R.id.et_doctor_code);
        et_password1 = findView(R.id.et_password1);
        et_password2 = findView(R.id.et_password2);
        iv_phone_code_delete = findView(R.id.iv_phone_code_delete);
        iv_doctor_code_delete = findView(R.id.iv_doctor_code_delete);
        iv_password1_delete = findView(R.id.iv_password1_delete);
        iv_password2_delete = findView(R.id.iv_password2_delete);
        but_getCode_again = findView(R.id.but_getCode_again);
        but_register = findView(R.id.but_register);
        iv_phone_code_delete.setVisibility(View.GONE);
        iv_doctor_code_delete.setVisibility(View.GONE);
        iv_password1_delete.setVisibility(View.GONE);
        iv_password2_delete.setVisibility(View.GONE);
        titleBar =getTitleBar();
        titleBar.setTitle(R.string.register);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_return:
                tent2(Verification_codeActivity.class);
                break;
            case R.id.iv_phone_code_delete:
                et_phone_code.setText("");
                break;
            case R.id.iv_doctor_code_delete:
                et_doctor_code.setText("");
                break;
            case R.id.iv_password1_delete:
                et_password1.setText("");
                break;
            case R.id.iv_password2_delete:
                et_password2.setText("");
                break;
            case R.id.but_getCode_again:
                userController.getCode(et_phone.getText().toString(), 0);
                break;
            case R.id.but_register:
               userController.register(phone,  et_doctor_code.getText().toString(),et_phone_code.getText().toString(),et_password1.getText().toString(),et_password2.getText().toString());
                break;
        }
    }

    @Override
    public void loginSuccess(Staff staff) {

    }

    @Override
    public void registerSuccess() {

        tent2(LoginActivity.class);
    }

    @Override
    public void getCodeSuccess() {

    }

    @Override
    public void forgetpwdSuccess() {

    }
    public void runOnBackGround(final Runnable runnable){
        new AsyncTask() {
            @Override
            protected Object doInBackground(Object[] params) {
                new  Thread(){

                    int timer=60;
                    @Override
                    public void run() {
                        while (timer>=0){
                            timer-=1;
                            try {
                                Thread.sleep(1000);
                            }catch (InterruptedException e){
                                e.printStackTrace();
                            }
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if (timer>0){
                                        but_getCode_again.setText(""+timer+"秒");
                                        but_getCode_again.setClickable(false);
                                        but_getCode_again.setSelected(true);
                                    }else {
                                        but_getCode_again.setText("重新获取");
                                        but_getCode_again.setClickable(true);
                                        but_getCode_again.setSelected(false);
                                    }
                                }
                            });
                        }
                    }

                }.run();

                return null;
            }
        }.execute();
    }
}
